<template>
    <div>
    <div class="demo-upload-list" v-for="item in uploadList">
        <template v-if="item.status === 'finished'">
            <img :src="item.url">
            <div class="demo-upload-list-cover">
                <Icon type="ios-eye-outline" @click.native="handleView(item.url)"></Icon>
            </div>
        </template>
        <template v-else>
            <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
        </template>
    </div>
    <Upload
        ref="upload"
        type="drag"
        :show-upload-list="false"
        :with-credentials="true"
        :default-file-list="defaultList"
        :on-success="handleSuccess"
        :format="['jpg','jpeg','png']"
        :max-size="4096"
        :on-format-error="handleFormatError"
        :on-exceeded-size="handleMaxSize"
        :before-upload="handleBeforeUpload"
        :data = "reqdata"
        action="/apidl/aiconfig/pic"
        style="display: inline-block;width:58px;">
        <div style="width: 58px;height:58px;line-height: 58px;">
            <Icon type="ios-camera" size="20"></Icon>
        </div>
    </Upload>
    <Modal title="图片查看" v-model="visible">
        <img :src="imgurl" v-if="visible" style="width: 100%">
    </Modal>
    </div>
</template>
<script>
    import { uploadImgApi } from '@/api/data'
    import { getAuthkey, getAccess } from '@/libs/util'
    export default {
        data () {
            return {
                defaultList: [],
                imgparam:{},
                shareImgList: { share01: '', share02: '', share03: '', share04: '', share05: '' },
                imgName: '',
                reqdata: {auth_key: getAuthkey(), file: FormData, name: String},
                visible: false,
                uploadList: []
            }
        },
        props:{
            imgurl: {
                type: String,
                default: ''
            },
            tag: {
                type: String,
                default: ''
            }
        },
        created(){
            if(this.imgurl){
                this.defaultList = [{
                    'url': this.imgurl
                }]
            }else{
                this.defaultList = []
            }
        },
        methods: {
            handleView (url) {
                this.imgurl = url;
                this.visible = true;
            },
            handleRemove (file) {
                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
            },
            handleSuccess (res, file) {
                if(res.code !== 200){
                    this.$Message.error(res.data.error)
                    return
                }
                this.shareImgList[this.tag] = res.data;
                this.$emit('uploadaction',this.shareImgList)
                file.url = res.data;
                this.uploadList = [];
                this.$set(this.uploadList, 0, file);
            },
            handleFormatError (file) {
                this.$Notice.warning({
                    title: '文件格式错误',
                    desc: '文件' + file.name + '格式错误，请选择.jpg文件或者.png文件'
                });
            },
            handleMaxSize (file) {
                this.$Notice.warning({
                    title: '文件大小限制',
                    desc: '文件  ' + file.name + ' 太大，超过了4M'
                });
            },
            // 上传之前钩子，如果需要自己定义上传方式则在方法内返回false，停止上传
            handleBeforeUpload (file) {
                let data = {
                    auth_key: getAuthkey(),
                    file: file,
                    name: file.name
                }
                this.reqdata = data;
                //this.uploadAction(data)
                //return false
            },
            uploadAction (data){
                console.log(data)
                uploadImgApi(data).then(res => {
                    if(res.data.code !== 200){
                        this.$Message.error(res.data.error)
                        return
                    }
                }, err => {
                    this.$Message.error(err? err: '上传失败')
                })
            }
        },
        mounted () {
            this.uploadList = this.$refs.upload.fileList;
        }
    }
</script>
<style>
    .demo-upload-list{
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0,0,0,.2);
        margin-right: 4px;
    }
    .demo-upload-list img{
        width: 100%;
        height: 100%;
    }
    .demo-upload-list-cover{
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0,0,0,.6);
    }
    .demo-upload-list:hover .demo-upload-list-cover{
        display: block;
    }
    .demo-upload-list-cover i{
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }
</style>